package com.ys.controller.api.finance;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.entity.finance.TcwPointRewardPunish;
import com.ys.entity.finance.TcwRunningBack;
import com.ys.service.finance.TcwPointRewardPunishService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tcwPointRewardPunish")
public class ApiInterTcwPointRewardPunishController {
	
	public static Long MENU_ID = 10160L;
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private TcwPointRewardPunishService tcwPointRewardPunishService;
	@Autowired
	private YspTaskService yspTaskService;
	
	@Autowired
	private CommonApiService<TcwPointRewardPunish> commonApiServiceDetail;

	@RequiresPermissions("tcwPointRewardPunish:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TcwPointRewardPunish cond){
		
		cond.setOrderby("A.Id desc");
		cond.setStatus(1);
		
		LayuiTableVoJA vo = commonApiServiceDetail.searchLay(cond);
		return vo;
	}
	
	/**
	 * 新增
	 * @throws Exception 
	 */
	@RequiresPermissions("tcwPointRewardPunish:add")
	@PostMapping("/add")
    @ResponseBody
	public R add(String bid, BigDecimal amount, 
			Integer type,
			String itemName,
			String itemNameSub,
			String remark,
			String userId,
			String userName,
			Long roleId,
			String roleName,
			String orgCode,
			String orgName) throws Exception{
		
		return tcwPointRewardPunishService.add(bid, amount, type, itemName, itemNameSub, remark, userId, userName, roleId, roleName, orgCode, orgName, MENU_ID);
	}
	
	/**
	 * 流转通过
	 */
	@RequiresPermissions("tcwPointRewardPunish:flow")
	@PostMapping("/flowPass")
    @ResponseBody
	public R flowPass(HttpServletRequest request, Long id){
		try {
			return tcwPointRewardPunishService.flowPass(id, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 流转驳回
	 */
	@RequiresPermissions("tcwPointRewardPunish:flow")
	@PostMapping("/flowRefuse")
    @ResponseBody
	public R flowRefuse(HttpServletRequest request, Long id, String reason){
		
		TcwRunningBack entity = new TcwRunningBack().setId(id).queryFirst();
		String bid = entity.getBid();
		try {
			return yspTaskService.flowRefuse(bid, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 删除
	 * @throws Exception 
	 */
	@RequiresPermissions("tcwPointRewardPunish:delete")
	@PostMapping("/delete")
    @ResponseBody
	public R delete(Long id){
		
		try {
			return tcwPointRewardPunishService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("操作失败，请联系客服");
		}
	}
}
